package com.rainng.coursesystem.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rainng.coursesystem.dao.mapper.ActivityMapper;
import com.rainng.coursesystem.model.entity.ActivityEntity;
import com.rainng.coursesystem.model.vo.response.table.ActivityItemVO;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ActivityDAO extends BaseDAO {
    public static final int PAGE_SIZE = 20;

    private final ActivityMapper mapper;

    public ActivityDAO(ActivityMapper mapper) {
        this.mapper = mapper;
    }

    public int insert(ActivityEntity entity) {
        return mapper.insert(entity);
    }

    public int delete(Integer id) {
        return mapper.deleteById(id);
    }

    public ActivityEntity get(Integer id) {
        return mapper.selectById(id);
    }

    public int update(ActivityEntity entity) {
        return mapper.updateById(entity);
    }

    public int count(String areaName, String name) {
        return mapper.count(areaName, name);
    }

    public List<ActivityItemVO> getPage(Integer index, String areaName, String name) {
        Page<ActivityItemVO> page = new Page<>(index, PAGE_SIZE);

        return mapper.getPage(page, areaName, name).getRecords();
    }

    public Integer countByAreaId(Integer areaId) {
        LambdaQueryWrapper<ActivityEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ActivityEntity::getAreaId, areaId);

        return mapper.selectCount(wrapper);
    }

    public List<ActivityEntity> listName() {
        LambdaQueryWrapper<ActivityEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(ActivityEntity::getId, ActivityEntity::getName);

        return mapper.selectList(wrapper);
    }
}
